package cn.yangliu.nacos.oauth2.repository;

import cn.yangliu.jpa.base.repository.BaseRepository;
import cn.yangliu.nacos.oauth2.entity.User;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

/**
 * The interface User repository.
 *
 * @author 问道于盲
 * @date 2019 -12-11
 */
public interface UserRepository extends BaseRepository<User, Long> {

    /**
     * Find by username user.
     *
     * @param username the username
     * @return the user
     */
    User findByUsername(String username);

    /**
     * Update password int.
     *
     * @param password the password
     * @param id       the id
     * @return the int
     */
    @Query(value = "update oauth2_user set password = :password where id = :id",
            nativeQuery = true)
    @Modifying
    int updatePassword(String password, Long id);

    /**
     * Delete user by id int.
     *
     * @param id the id
     * @return the int
     */
    @Modifying
    int deleteUserById(Long id);

    /**
     * Count by username long.
     *
     * @param username the username
     * @return the long
     */
    long countByUsername(String username);

    /**
     * Count by mobile long.
     *
     * @param mobile the mobile
     * @return the long
     */
    long countByMobile(String mobile);

    /**
     * Count by email long.
     *
     * @param email the email
     * @return the long
     */
    long countByEmail(String email);

    /**
     * Count by mobile and id is not long.
     *
     * @param mobile the mobile
     * @param id     the id
     * @return the long
     */
    long countByMobileAndIdIsNot(String mobile, Long id);

    /**
     * Count by email and id is not long.
     *
     * @param email the email
     * @param id    the id
     * @return the long
     */
    long countByEmailAndIdIsNot(String email, Long id);
}
